package com.zx.map.dao.impl;

import com.zx.map.dao.TodoTaskDao;
import com.zx.map.dao.mapper.TodoTaskMapper;
import com.zx.map.dao.model.TodoTask;
import com.zx.map.dao.model.TodoTaskExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-1
 * Time: 上午10:54
 * To change this template use File | Settings | File Templates.
 */
@Component("TodoTaskDao")
public class TodoTaskImpl implements TodoTaskDao {

    @Autowired
    private TodoTaskMapper mapper;

    /**
     * 新增
     */
    public Integer add(TodoTask todoTask) throws DataAccessException {
    	todoTask.setCreateDate(new Date());
        int column = mapper.insert(todoTask);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return todoTask.getTaskId();
    }

    /**
     * 修改
     * @param todoTask
     * @return
     * @throws DataAccessException
     */
    public Integer update(TodoTask todoTask) throws DataAccessException {
    	todoTask.setUpdateDate(new Date());
        int column = mapper.updateByPrimaryKeySelective(todoTask);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return todoTask.getTaskId();
    }

    /**
     * 删除
     */
    public void delete(TodoTask todoTask) throws DataAccessException {
        int column = mapper.deleteByPrimaryKey(todoTask.getTaskId());
        if (column != 1) {
            throw new RuntimeException("");
        }
    }

    /**
     * 分页查找
     */
	public List<TodoTask> getPageByExample(int pageSize, int pageBegin,
			TodoTaskExample example) throws DataAccessException {
		return mapper.getPageByModel(pageSize, pageBegin, example); 
	}
	/**
	 * 查找条数
	 * 
	 * @param example
	 * @return
	 * @throws DataAccessException
	 */
	public int countByExample(TodoTaskExample example) throws DataAccessException {
		return mapper.countByExample(example);
	}

	/**
	 * 
	 */
	public List<TodoTask> getListByExample(TodoTaskExample example)
			throws DataAccessException {
		return mapper.selectByExample(example); 
	}

	public TodoTask selectByPrimaryKey(Integer key) throws DataAccessException {
		return mapper.selectByPrimaryKey(key);
	}
}
